package org.kzwl.service.web.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.kzwl.comm.advice.BizException;
import org.kzwl.comm.advice.Consts;
import org.kzwl.comm.pojo.ResponseData;
import org.kzwl.comm.pojo.bo.AdminQueryNewsBO;
import org.kzwl.comm.pojo.bo.NewsBO;
import org.kzwl.comm.pojo.bo.news.NewsBasicBO;
import org.kzwl.comm.pojo.bo.news.QueryDetailsBO;
import org.kzwl.comm.pojo.dto.news.NewsDTO;
import org.kzwl.comm.utils.MyUtils;
import org.kzwl.entity.web.News;
import org.kzwl.mapper.web.NewsMapper;
import org.kzwl.service.web.NewsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {

    @Autowired
    private NewsMapper newsMapper;

    @Override
    public ResponseData<Void> saveOne(NewsBO newsBO) {
        News news = new News();
        BeanUtils.copyProperties(newsBO, news);
        this.save(news);
        return new ResponseData<>();
    }

    @Override
    public ResponseData<IPage<News>> adminQuery(AdminQueryNewsBO newsBO) {

        QueryWrapper<News> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderBy(true, false, News::getCuNewsDate);
        Page<News> newsPage = this.baseMapper.selectPage(new Page<>(newsBO.getCurrent(), newsBO.getSize()), new QueryWrapper<News>().lambda().eq(News::getCuTownId, newsBO.getCuTownId()));
        return new ResponseData<>(newsPage);
    }

    @Override
    public ResponseData<News> queryOne(Long Id) {
        News selectById = this.baseMapper.selectById(Id);
        if (selectById == null) {
            throw new BizException("新闻不存在");
        }
        return new ResponseData<>(selectById);
    }

    @Override
    public ResponseData<Void> updateOne(NewsBO newsBO) {
        News selectById = this.baseMapper.selectById(newsBO.getCuNewsId());
        if (selectById == null) {
            throw new BizException("新闻不存在");
        }
        News news = new News();
        BeanUtils.copyProperties(newsBO, news);
        this.baseMapper.updateById(news);
        return new ResponseData<>();
    }

    @Override
    public ResponseData<Void> deleteId(Long Id) {
        News selectById = this.baseMapper.selectById(Id);
        if (selectById == null) {
            throw new BizException("新闻不存在");
        }
        this.baseMapper.deleteById(Id);
        return new ResponseData<>();
    }

    //region 查询首页新闻
    @Override
    public List<NewsDTO> indexQuery(NewsBasicBO bo) {
        QueryWrapper<News> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(bo.getCuCategoryId() != 0, News::getCuCategoryId, bo.getCuCategoryId())
                .eq(News::getCuTownId,bo.getCuTownId())
                .orderByDesc(News::getCuNewsDate).last(" limit 0,10");
        List<News> list = newsMapper.selectList(queryWrapper);
        List<NewsDTO> dtos = JSONObject.parseArray(JSONObject.toJSONString(list), NewsDTO.class);
        dtos.forEach(l -> {
            l.setCuNewsContent(l.getCuNewsContent().substring(0, Math.min(l.getCuNewsContent().length(), 30)));
        });
        return dtos;
    }
    //endregion

    //region app接口-基础查询
    @Override
    public IPage<NewsDTO> queryBasic(NewsBasicBO bo) {
        QueryWrapper<News> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(bo.getCuCategoryId() != 0, News::getCuCategoryId, bo.getCuCategoryId())
                .eq(News::getCuTownId,bo.getCuTownId())
                .orderByDesc(News::getCuNewsDate);
        IPage<News> page = new Page<>();
        page.setCurrent(bo.getCurrent()).setSize(bo.getSize());
        page = newsMapper.selectPage(page, queryWrapper);
        List<NewsDTO> list = JSONObject.parseArray(JSONObject.toJSONString(page.getRecords()), NewsDTO.class);
        IPage<NewsDTO> news = new Page<>();
        BeanUtils.copyProperties(page, news);
        news.setRecords(list);
        return news;
    }
    //endregion

    //region 查询新闻详细
    @Override
    public NewsDTO queryDetails(QueryDetailsBO newsBO) {
        News news = newsMapper.selectById(newsBO.getCuNewsId());
        NewsDTO dto = new NewsDTO();
        BeanUtils.copyProperties(news, dto);
        return dto;
    }
    //endregion

    //region app查询最新资讯
    @Override
    public List<NewsDTO> queryNews(NewsBasicBO newsBO) {
        QueryWrapper<News> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(News::getCuNewsDate).eq(News::getCuTownId,1);
        IPage<News> page = new Page<>();
        page.setCurrent(newsBO.getCurrent());
        page.setSize(newsBO.getSize());
        page = newsMapper.selectPage(page, queryWrapper);
        List<NewsDTO> list = JSONObject.parseArray(JSONObject.toJSONString(page.getRecords()), NewsDTO.class);
        list.forEach(l->{
            l.setCuNewsContent("");
            l.setCuNewsUrl(Consts.MyConsts.DetailsUrl+"news/"+l.getCuNewsId());
        });
        return list;
    }
    //endregion

}
